package com.cne.gmail.Gmail.Handle;



import com.cne.gmail.Gmail.model.AttchmentModel;
import com.cne.gmail.Gmail.model.ContextImgModel;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.URLDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class MessageHander {

    /**
     * @param body
     * @param contextImgModels
     * @param multipart
     * @throws Exception
     */

    public static void createContent(String body, List<ContextImgModel> contextImgModels, Multipart multipart)
            throws Exception {
        // 用于组合文本和图片，"related"型的MimeMultipart对象
        MimeBodyPart contentBody = new MimeBodyPart();
        MimeBodyPart contentBodyText = new MimeBodyPart();
        //建立文字与图片的关系
        MimeMultipart contentMulti = new MimeMultipart("related");
        //html与text文本格式
        MimeMultipart contentMultiText = new MimeMultipart("alternative");

        //正文的文本部分plain格式
        MimeBodyPart plainBody = new MimeBodyPart();
        plainBody.setContent(body, "text/plain;charset=UTF-8");
        contentMultiText.addBodyPart(plainBody);
        // 正文的文本部分html格式
        MimeBodyPart htmlBody = new MimeBodyPart();
        htmlBody.setContent(body, "text/html;charset=UTF-8");
        contentMultiText.addBodyPart(htmlBody);
        contentBodyText.setContent(contentMultiText);
        contentMulti.addBodyPart(contentBodyText);
        // 正文的图片部分
        int length = contextImgModels == null ? 0 : contextImgModels.size();
        URL url;
        for (int i=0;i<length;i++){
             url=new URL(contextImgModels.get(i).getUrl());
             MimeBodyPart jpgBody = new MimeBodyPart();
             DataSource dataSource=new URLDataSource(url);
             jpgBody.setDataHandler(new DataHandler(dataSource));
             jpgBody.setFileName(contextImgModels.get(i).getFileName());
             jpgBody.setHeader("Content-ID", "<"+contextImgModels.get(i).getcId()+">");
             contentMulti.addBodyPart(jpgBody);
          }
        contentBody.setContent(contentMulti);
        //最后一步，添加related部分
        multipart.addBodyPart(contentBody);
    }

    /**
     * 添加附件处理
     * @param attchmentModels
     * @param multipart
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    public static void attchHandler(List<AttchmentModel> attchmentModels, MimeMultipart multipart) throws MessagingException, MalformedURLException {
        int leng = attchmentModels == null ? 0 : attchmentModels.size();
        URL url;
        for (int i = 0; i < leng; i++) {
            // 根据文件名获取数据源
            url=new URL(attchmentModels.get(i).getUrl());
            MimeBodyPart messageBodyPart2 = new MimeBodyPart();
            DataSource dataSource=new URLDataSource(url);
            messageBodyPart2.setDataHandler(new DataHandler(dataSource));
            messageBodyPart2.setFileName(attchmentModels.get(i).getFileName());
            multipart.addBodyPart(messageBodyPart2);
        }
    }
    /**
     * 添加发件人和抄送人
     * @param tos
     * @param toCCs
     * @param mimeMessage
     * @throws MessagingException
     */
    public static void ToandCc(List<String> tos, List<String> toCCs, MimeMessage mimeMessage) throws MessagingException {
        //添加收件人多个收件人
        if(tos != null && tos.size() != 0){
           InternetAddress[] to = new InternetAddress[tos.size()];
            // 设置邮件消息的发送者
            for (int i = 0; i < tos.size(); i++) {
                to[i] = new InternetAddress(tos.get(i));
            }
            mimeMessage.setRecipients(Message.RecipientType.TO, to);
        }
        //添加多个抄送人
        if(toCCs != null && toCCs.size() != 0){
            InternetAddress[] toCC = new InternetAddress[toCCs.size()];
            // 设置邮件消息的发送者
            for (int i = 0; i < toCCs.size(); i++) {
                toCC[i] = new InternetAddress(toCCs.get(i));
            }
            mimeMessage.addRecipients(Message.RecipientType.CC, toCC);
        }
    }

}
